package scripts;
import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class DraynorCerter extends ScriptMethods implements Runnable {

	private boolean uncerting;
	private String name;
	private String type;
	private int certerID;
	private int itemID;

	public void start() {

		getAccountSettings();
		System.gc();

		Object[] options = {"Cert", "Uncert"};
		Object[] options2 = {"Raw lobster", "Cooked lobster", "Raw swordfish", "Cooked swordfish",
		"Iron bar", "Silver bar", "Gold bar", "Mithril bar", "Iron ore", "Coal ore", "Silver ore", "Gold ore",
		"Mithril ore"};

		String res1 = (String)JOptionPane.showInputDialog(null, "", "Draynor Certer", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
		String res2 = (String)JOptionPane.showInputDialog(null, "", "Draynor Certer", JOptionPane.PLAIN_MESSAGE, null, options2, options2[0]);

		if (res1.equals("Cert")) {
			uncerting = false;
		} else if (res1.equals("Uncert")) {
			uncerting = true;
		}

		System.out.println(res1 + "ing" + " " + res2 + "s.");

		if (res2.equals("Raw lobster")) {
			itemID = 372;
			certerID = 227;
			name = "Raw Lobster";
			type = "fish";
		} else if (res2.equals("Cooked lobster")) {
			itemID = 373;
			certerID = 227;
			name = "Lobster";
			type = "fish";
		} else if (res2.equals("Raw swordfish")) {
			itemID = 369;
			certerID = 227;
			name = "Raw swordfish";
			type = "fish";
		} else if (res2.equals("Cooked swordfish")) {
			itemID = 370;
			certerID = 227;
			name = "swordfish";
			type = "fish";
		}

		if (res2.equals("Iron bar")) {
			itemID = 170;
			certerID = 226;
			name = "iron bar";
			type = "bars";
		} else if (res2.equals("Silver bar")) {
			itemID = 384;
			certerID = 226;
			name = "silver bar";
			type = "bars";
		} else if (res2.equals("Steel bar")) {
			itemID = 171;
			certerID = 226;
			name = "steel bar";
			type = "bars";
		} else if (res2.equals("Gold bar")) {
			itemID = 172;
			certerID = 226;
			name = "Gold bar";
			type = "bars";
		} else if (res2.equals("Mithril bar")) {
			itemID = 173;
			certerID = 226;
			name = "mithril bar";
			type = "bars";
		}

		if (res2.equals("Iron ore")) {
			itemID = 151;
			certerID = 225;
			name = "iron";
			type = "ore";
		} else if (res2.equals("Coal ore")) {
			itemID = 155;
			certerID = 225;
			name = "Coal";
			type = "ore";
		} else if (res2.equals("Silver ore")) {
			itemID = 383;
			certerID = 225;
			name = "silver";
			type = "ore";
		} else if (res2.equals("Gold ore")) {
			itemID = 152;
			certerID = 225;
			name = "Gold";
			type = "ore";
		} else if (res2.equals("Mithril ore")) {
			itemID = 153;
			certerID = 225;
			name = "mithril";
			type = "ore";
		}
		
		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptThread = null;
		scriptRunning = false;
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();
				int[] bankDoor = getObjectById(64);
				if (bankDoor[0] != -1) {
					atObject(bankDoor[1], bankDoor[2]);
					wait(random(1000, 1200));
				}
				if (bankOpen()) {
					if (uncerting) {
						depositAll(itemID);
						wait(600);
						closeBank();
					} else {
						withdraw(itemID, 25);
						wait(600);
						closeBank();
					}
				}
				if (uncerting) {
					if (questMenuOpen()) {
						answerQuestMenuStringContains("access", true);
						answerQuestMenuStringContains("cert", false);
						answerQuestMenuStringEquals(name, false);
						answerQuestMenuStringContains("five", true);
					}
					if (ourPlayerX() <= 223) { // Inside the bank
						if (getInventorySize() >= 25) {
							openBank();
							wait(2000);
						} else {
							if (certerID != 226) {
								walkTo(229, 630, false);
								wait(1000);
							} else if (certerID == 227) {
								walkTo(228, 627, false);
								wait(1000);
							} else {
								walkTo(229, 633, false);
								wait(1000);
							}
						}
					} else {
						if (getInventorySize() >= 25) {
							walkTo(220, 634, false);
							wait(1000);
						} else {
							talkToNpc(new int[] { certerID });
							wait(1000);
						}
					}
				} else {
					if (questMenuOpen()) {
						answerQuestMenuStringContains("access", true);
						answerQuestMenuStringContains(type + " to trade in", false);
						answerQuestMenuStringEquals(name, false);
						answerQuestMenuStringContains("Twentyfive", true);
					}
					if (ourPlayerX() <= 223) { // Inside the bank
						if (getInventorySize() >= 25) {
							if (certerID != 226) {
								walkTo(229, 630, false);
								wait(1000);
							} else if (certerID == 227) {
								walkTo(228, 627, false);
								wait(1000);
							} else {
								walkTo(229, 633, false);
								wait(1000);
							}
						} else {
							openBank();
							wait(2000);
						}
					} else {
						if (getInventorySize() >= 25) {
							talkToNpc(new int[] { certerID });
							wait(1200);
						} else {
							walkTo(220, 634, false);
							wait(1000);
						}
					}
				}
				wait(50);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
	}
}